package cn.chuanw.db.service;

import cn.chuanw.db.dao.WRoomInfoMapper;
import com.github.pagehelper.PageHelper;
import cn.chuanw.db.dao.WTenantInfoMapper;
import cn.chuanw.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoomService {
	@Resource
	private WRoomInfoMapper wRoomInfoMapper;

	@Resource
	private WTenantInfoMapper wTenantInfoMapper;

	public List<WRoomInfo> querySelective(String roomFullName, String houseId, Integer leaseStatus, Integer page,
			Integer size) {
		WRoomInfoExample wRoomInfoExample = new WRoomInfoExample();
		WRoomInfoExample.Criteria criteria = wRoomInfoExample.createCriteria();
		if (!StringUtils.isEmpty(roomFullName)) {
			criteria.andRoomFullNameLike("%" + roomFullName + "%");
		}
		if (!StringUtils.isEmpty(houseId)) {
			criteria.andHouseIdEqualTo(houseId);
		}
		if (!StringUtils.isEmpty(leaseStatus)) {
			criteria.andLeaseStatusEqualTo(leaseStatus);
		}
		this.playFlagUpdate();
		criteria.andDelFlagEqualTo(0);
		PageHelper.startPage(page, size);
		return wRoomInfoMapper.selectByExample(wRoomInfoExample);
	}

	public void addRoomInfo(WRoomInfo wRoomInfo) {
		wRoomInfoMapper.insertSelective(wRoomInfo);
	}

	public int updateById(WRoomInfo wRoomInfo) {
		return wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
	}

	/**
	 * @Auther: wangkun
	 * @Date: 20-02-08 下午 5:59
	 * @Description: 根据状态查询房间
	 **/
	public List<WRoomInfo> queryRoomByLeaseStatus(Integer leaseStatus) {
		WRoomInfoExample example = new WRoomInfoExample();
		WRoomInfoExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(leaseStatus)) {
			criteria.andLeaseStatusEqualTo(leaseStatus);
		}
		criteria.andDelFlagEqualTo(0);
		return wRoomInfoMapper.selectByExample(example);
	}


    public List<WRoomInfo> queryRoom() {
        WRoomInfoExample example = new WRoomInfoExample();
        WRoomInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(0);
        return wRoomInfoMapper.selectByExample(example);
    }

	public WRoomInfo queryById(String roomId) {
		return wRoomInfoMapper.selectByPrimaryKey(roomId);
	}

	public Object playFlagUpdate() {
	
	

		WRoomInfoExample wRoomInfoExample = new WRoomInfoExample();
		WRoomInfoExample.Criteria criteria = wRoomInfoExample.createCriteria();
		// 未删除已出租的房间参与计算当月是否收租
		criteria.andDelFlagEqualTo(0).andLeaseStatusEqualTo(1);
		List<WRoomInfo> wRoomInfos = wRoomInfoMapper.selectByExample(wRoomInfoExample);

		// 判断当日是否为当月一号
		if (LocalDateTime.now().getDayOfMonth() == 1) {
			int nowMonth = LocalDateTime.now().getMonthValue();
			int nowYear = LocalDateTime.now().getYear();
			// 获取自公元元年至今所有月份
			int allMonth = nowYear * 12 + nowMonth;
			for (WRoomInfo wRoomInfo : wRoomInfos) {
				// 0当月已支付 1当月未支付
				// 判断当日是否为凌晨1点 每个月凌晨一点改变当月支付状态为当月未支付
				if (LocalDateTime.now().getHour() == 1) {
					wRoomInfo.setMonthPlayFlag(1);
					// 每月一号，改变当前月支付状态为 当前月未支付
					wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
				}
				// 获取在租租户的信息
				WTenantInfoExample wTenantInfoExample = new WTenantInfoExample();
				WTenantInfoExample.Criteria wTenantcriteria = wTenantInfoExample.createCriteria();
				wTenantcriteria.andDelFlagEqualTo(0).andRoomIdEqualTo(wRoomInfo.getId());
				List<WTenantInfo> wTenantInfo = wTenantInfoMapper.selectByExample(wTenantInfoExample);
				if (wTenantInfo.size() > 0) {
					LocalDate beginDate = wTenantInfo.get(0).getBeginDate();
					// 获取房屋支付方式 1押一付一 2押一付二 3押一付三 4半年付 5全款付清
					int cycle = wTenantInfo.get(0).getRentCycle();
					// 判断开始时间是否为空
					if (beginDate != null) {
						int month = beginDate.getMonthValue();
						int yer = beginDate.getYear();

						// 获取公元元年至起租时间所有月份，
						int beginAllMonth = yer * 12 + month;
						if (allMonth >= beginAllMonth) {
							// 已租所有月份
							int pastMonth = allMonth - beginAllMonth;
							// 余出的月份，整除说明 需要交租 押一付一绝对整除表示每月交一次，每个月都显示一次，押一付而
							// 余一表示下个月要交租，押一付三，余几就是剩余几个月要交租
							int surplusMonth = pastMonth % cycle;

							// 交租状态 0当月需支付 1 当月不需支付surplusMonth==0 当月需支付
							if (surplusMonth == 0) {
								wRoomInfo.setRoomPlayFlag(0);
								wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
							} else if(surplusMonth == 1){
								wRoomInfo.setRoomPlayFlag(1);
								wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
							}else if(surplusMonth == 2){
								wRoomInfo.setRoomPlayFlag(2);
								wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
							}
						}
					}

				}

			}
		}
		return wRoomInfos;

	}
}
